package me.macd.mindoc.controller;

import me.macd.mindoc.model.Members;
import me.macd.mindoc.util.constants.Constants;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: mindoc
 * @description: ${description}
 * @author: 2018-09-23
 * @create: 2018-09-23 10:57
 */
public class Common {
    public static Map<String, Object> map(String controllerName, String actionName) {
        Map<String, Object> map = new HashMap<>();
        map.put("SITE_TITLE", "MinDoc");
        map.put("SITE_NAME", "MinDoc");
        map.put("ControllerName", controllerName);
        map.put("ActionName", actionName);
        map.put("Keyword", "");
        map.put("Member", currentMember());

        return map;
    }

    public static Members currentMember() {
        Members members = new Members();
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Object object = subject.getSession().getAttribute(Constants.SESSION_USER_INFO);
            if (object != null) {
                members = (Members) object;
            }
        }
        return members;
    }

    public static boolean isLogined() {
        Subject subject = SecurityUtils.getSubject();
        return subject.isAuthenticated();
    }

    public static int memberId() {
        int memberId = 0;
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Object object = subject.getSession().getAttribute(Constants.SESSION_USER_INFO);
            if (object != null) {
                memberId = ((Members) object).getMemberId();
            }
        }
        return memberId;
    }
}
